import matplotlib.pyplot as plt
import numpy as np

# 数据
data_2022 = [0.109, 0.250]
data_2023 = [0.145, 0.281]

# 指标标签
labels = ["Operating Net Cash Flow/Operating Revenue", "Sales Net Cash Flow/Operating Revenue"]

# 设置中文字体为黑体
plt.rcParams["font.sans-serif"] = ["SimHei"]
# 解决负号显示问题
plt.rcParams["axes.unicode_minus"] = False

# 生成x轴坐标
x = np.arange(len(labels))

# 绘制瀑布图
fig, ax = plt.subplots()

# 绘制2022年数据
bottoms_2022 = [0] * len(labels)
for i in range(len(labels)):
    rect_2022 = ax.bar(x[i], data_2022[i], label = "2022" if i == 0 else "", bottom = bottoms_2022[i])
    bottoms_2022[i] += data_2022[i]

# 绘制2023年数据
bottoms_2023 = [0] * len(labels)
for i in range(len(labels)):
    rect_2023 = ax.bar(x[i], data_2023[i] - data_2022[i], bottom = bottoms_2022[i], label = "2023" if i == 0 else "")
    bottoms_2023[i] += data_2023[i]

# 设置x轴标签
ax.set_xticks(x)
ax.set_xticklabels(labels, rotation = 45)

# 设置y轴标签
ax.set_ylabel("Ratio")

# 设置图表标题
ax.set_title("Cash Flow Analysis")

# 添加图例
ax.legend()

# 显示图表
plt.tight_layout()
plt.show()